﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Player : GameComponent, whereIsTheDonutGame.IPlayer
    {

        private Vector3 position;
        public Vector3 Position
        {
            get { return position; }
        }

        private float rotation;
        public float Rotation
        {
            get { return rotation; }
        }
      
        public Player(Game game)
            : base(game)
        {
            this.position = new Vector3(0, 80, 0);
            this.rotation = 0f;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();

            //WASD
            if (state.IsKeyDown(Keys.W))
            {
                move(new Vector3(0, 0, -10));
            }
            else if (state.IsKeyDown(Keys.S))
            {
                move(new Vector3(0, 0, 10));
            }
            if (state.IsKeyDown(Keys.A))
            {
                move(new Vector3(-10, 0, 0));
            }
            else if (state.IsKeyDown(Keys.D))
            {
                move(new Vector3(10, 0, 0));
            }
            //Camera

            if (state.IsKeyDown(Keys.Right))
            {
                rotate(-0.05f);
            }
            else if (state.IsKeyDown(Keys.Left))
            {
                rotate(0.05f);
            }

            //HochRunter
            if (state.IsKeyDown(Keys.Space))
            {
                move(new Vector3(0, 10, 0));
            }
            else if (state.IsKeyDown(Keys.LeftControl))
            {
                move(new Vector3(0, -10, 0));
            }

            base.Update(gameTime);
        }

        private void move(Vector3 translation)
        {
            // Extract the rotation matrix that reflects the direction we are facing.
            Matrix forwardMovement = Matrix.CreateRotationY(Rotation);
         
            // Then reposition ourselves according to the direction we are facing.
            position += Vector3.Transform(translation, forwardMovement);
        }

        private void rotate(float rotationSpeed)
        {
            rotation += rotationSpeed;
        }

    }
}